import NewsDetail from '../newsDetail/newsDetail-model.js';
const NewsDetailModel = new NewsDetail();

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_id: 0,
    parent_id: 0,
    news_id: 0,
    more: true,
    inputValue: '',
    comments: [],
    toView: '',
    page: 1,
    size: 10,
    moreText: '已显示全部回复',
    actionSheetHidden: true,
    targetUser: {},
    hidePt: true,
    focus: false,
    replyStatus: true,
    height: 0,
    shoucangStatus: false,
    comment_count: 0,
    newsDetail: {},
    hideShade: true,
    drawHeight: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      drawHeight: app.globalData.drawHeight
    });
    var collections = wx.getStorageSync('collections');
    if (collections) {
      collections.map((item, index) => {
        if (item.news_id === options.news_id) {
          this.setData({
            shoucangStatus: true
          })

        }
      })
    }
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          height: res.windowHeight
        });
      }
    });

    var jsonstr = decodeURIComponent(options.newsDetail);
    var newsDetail = JSON.parse(jsonstr);
    this.setData({
      user_id: options.user_id,
      parent_id: options.parent_id,
      news_id: options.news_id,
      comment_count: options.comment_count,
      newsDetail: newsDetail
    })

    this._loadData();
  },
  _loadData() {
    NewsDetailModel.getComments({
      news_id: this.data.news_id,
      comment_id: this.data.parent_id
    }, 1, 1, (res) => {
      res.list.map((item, index) => {
        item.create_time = NewsDetailModel.formatTime(item.create_time);
      })
      this.setData({
        targetUser: res.list[0]
      })
    })
    this.getCommentsList(true);
  },
  onFacusComment() {
    this.setData({
      focus: true
    })
  },
  shareToMoments(event) {

    this.setData({
      hideShade: false
    })
    app.shareToMoments();
  },
  closeShade(event) {
    this.setData({
      hideShade: true
    })
  },
  //保存图片到本地
  saveImage() {
    app.canvasToImage();
  },
  //获取评论列表
  getCommentsList(init) {
    if (init) {
      this.setData({
        page: 1,
        more: true
      })
    }
    NewsDetailModel.getComments({
      news_id: this.data.news_id,
      parent_id: this.data.parent_id
    }, this.data.page, this.data.size, (res) => {
      if (res.list.length >= 1) {
        this.setData({
          hidePt: false
        })
      }
      res.list.map((item, index) => {
        item.create_time = NewsDetailModel.formatTime(item.create_time);
      })
      if (res.list.length < 1) {
        this.setData({
          more: false
        })
        return;
      }
      if (init) {
        this.setData({
          more: true,
          comments: res.list
        })
      } else {
        this.setData({
          more: true,
          comments: this.data.comments.concat(res.list)
        })
      }
    })
  },
  //收藏
  shoucang() {
    var collections = wx.getStorageSync('collections');
    if (!collections) {
      collections = new Array();
    }
    collections.push({
      news_id: this.data.news_id,
      newsDetail: this.data.newsDetail
    });
    wx.setStorageSync('collections', collections);
    this.setData({
      shoucangStatus: true
    });
    app.globalData.shoucangStatus = true;

  },
  //删除收藏
  delshoucang() {
    var collections = wx.getStorageSync('collections');
    var key = collections.indexOf({
      'news_id': this.data.news_id
    })
    collections.splice(key, 1);
    wx.setStorageSync('collections', collections);
    this.setData({
      shoucangStatus: false
    });
    app.globalData.shoucangStatus = false;
  },
  //发布评论
  writeSubmit(e) {
    var value = e.detail.value;
    if (value.trim() === '') {
      wx.showToast({
        title: '请输入评论内容！',
      });
      return;
    }
    NewsDetailModel.postComment({
      news_id: this.data.news_id,
      parent_id: this.data.parent_id,
      to_user_id: this.data.user_id,
      content: value
    }, (res) => {

      if (res.status === 1) {
        var ar = new Array(res.data);
        this.setData({
          comments: ar.concat(this.data.comments),
          toView: 'pl',
          inputValue: ''
        });

      } else {
        wx.showToast({
          title: '您还没有登陆哦！',
        });
        this.setData({
          actionSheetHidden: false
        })
      }

    })
  },
  //授权登陆
  onGotUserInfo(e) {
    var userInfo = e.detail.userInfo;
    if (userInfo) {
      NewsDetailModel.login(userInfo, (res) => {
        if (res.status === 1) {
          wx.setStorageSync('userInfo', res.data);
          wx.showToast({
            title: '登陆成功',
          })

        } else {
          wx.showToast({
            title: '登陆失败',
          })
        }
      });

    }
    this.setData({
      actionSheetHidden: true
    })
  },
  //登陆开关
  actionSheetChange() {
    this.setData({
      actionSheetHidden: true
    })
  },
  // 用户下拉刷新
  upper() {
    // this.getCommentsList(true);
  },

  // 用户上拉加载更多
  lower() {
    if (!this.data.more) {
      return false
    }
    this.setData({
      page: this.data.page + 1
    })
    this.getCommentsList(false);
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})